// pages/me/myposts/myposts.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    posts: [],
    openid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow() {
    wx.cloud.callFunction({ // 获取openid
      name: "getOpenid"
    }).then(res => {
      this.data.openid = res.result.openid
      this.getPost(0,this.data.openid)
    })

  },
  gotoPost(e){
    wx.navigateTo({
      url: '../../community/post/post?postid=' + e.currentTarget.dataset.postid,
    })
  },
  getPost(skiplength,openid) {
    wx.showLoading({
      title: '加载中'
    })
    wx.cloud.callFunction({
      name: 'getPosts',
      data: {
        openid: openid,
        skiplength: skiplength
      }
    })
      .then(res => {
        if (res.result.list.length <= 0) {
          wx.showToast({
            icon: 'none',
            title: '没有更多数据啦',
          })
          return;
        }
        wx.stopPullDownRefresh()
        if (skiplength == 0) {  // 重头刷新
          this.setData({
            posts: res.result.list
          })
        } else {         // 分页加载
          this.setData({
            posts: this.data.posts.concat(res.result.list)
          })
        }
        wx.hideLoading()
      })
      .catch(res => {
        console.log("请求失败", res)
      })
  },
  onPullDownRefresh() {
    this.getPost(0, this.data.openid)
  },
  onReachBottom() {
    this.getPost(this.data.posts.length,this.data.openid)
  },
})